~ubuntu-branches/ubuntu/natty/quickly/natty

« back to all changes in this revision

Viewing changes to quickly/tools.py

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-07-24 18:16:30 UTC
  • Revision ID: james.westby@ubuntu.com-20090724181630-s2wo7hvahk8u0hqb
Tags: 0.1
Initial release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright 2009 Canonical Ltd.
 
3
# Author 2009 Didier Roche
 
4
#
 
5
# This file is part of Quickly
 
6
#
 
7
#This program is free software: you can redistribute it and/or modify it 
 
8
#under the terms of the GNU General Public License version 3, as published 
 
9
#by the Free Software Foundation.
 
10
 
 
11
#This program is distributed in the hope that it will be useful, but 
 
12
#WITHOUT ANY WARRANTY; without even the implied warranties of 
 
13
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
14
#PURPOSE.  See the GNU General Public License for more details.
 
15
 
 
16
#You should have received a copy of the GNU General Public License along 
 
17
#with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import os
 
20
import string
 
21
import sys
 
22
import stat
 
23
 
 
24
import gettext
 
25
from gettext import gettext as _
 
26
 
 
27
from quickly import quicklyconfig
 
28
 
 
29
def quickly_name(name):
 
30
    """Enforce quickly name to be ascii only and lowercase
 
31
    
 
32
    return formated name"""
 
33
    name = name.lower()
 
34
    permitted_characters = string.ascii_lowercase
 
35
    permitted_characters += "_"
 
36
    for c in name:
 
37
        if c not in permitted_characters:
 
38
            print _("""
 
39
ERROR: unpermitted character in name.
 
40
Letters and underscore ("_") only.""")
 
41
            sys.exit(1)
 
42
    return name
 
43
 
 
44
 
 
45
class project_path_not_found(Exception):
 
46
    pass
 
47
 
 
48
def get_quickly_data_path():
 
49
    """Retrieve quickly data path
 
50
 
 
51
    This path is by default <quickly_lib_path>/../data/ in trunk
 
52
    and /usr/share/quickly in an installed version but this path
 
53
    is specified at installation time.
 
54
    """
 
55
 
 
56
    # get pathname absolute or relative
 
57
    if quicklyconfig.__quickly_data_directory__.startswith('/'):
 
58
        pathname = quicklyconfig.__quickly_data_directory__
 
59
    else:
 
60
        pathname = os.path.dirname(__file__) + '/' + quicklyconfig.__quickly_data_directory__
 
61
    abs_data_path = os.path.abspath(pathname)
 
62
 
 
63
    if os.path.exists(abs_data_path):
 
64
        return abs_data_path
 
65
    else:
 
66
        raise project_path_not_found
 
67
 
 
68
def get_template_directories():
 
69
    """Retrieve all directories where quickly templates are
 
70
 
 
71
    :return a list of directories
 
72
    """
 
73
 
 
74
    # default to looking up templates in the current dir
 
75
    template_directories = []
 
76
    if os.path.exists(os.path.expanduser('~/.quickly-data/templates')):
 
77
        template_directories.append(os.path.expanduser('~/.quickly-data/templates'))
 
78
 
 
79
    # retrieve from trunk or installed version
 
80
    abs_template_path = get_quickly_data_path() + '/templates/'
 
81
    if os.path.exists(abs_template_path):
 
82
        template_directories.append(abs_template_path)
 
83
 
 
84
    if not template_directories:
 
85
        print _("No template directory found. Aborting")
 
86
        sys.exit(1)
 
87
 
 
88
    return template_directories
 
89
 
 
90
 
 
91
def get_template_directory(template):
 
92
    """Detect where the quickly template and if it exists"""
 
93
 
 
94
    # check for the first available template in template_directories
 
95
    for template_directory in get_template_directories():
 
96
        template_path = template_directory + "/" + template
 
97
        if os.path.exists(template_path):
 
98
            template_found = True
 
99
            break
 
100
        template_found = False
 
101
 
 
102
    # if still false, no template found in template_directories
 
103
    if not template_found:
 
104
        print _("ERROR: Template '%s' not found.") % template
 
105
        print _("Aborting")
 
106
        exit(1)
 
107
 
 
108
    return template_path
 
109
 
 
110
 
 
111
def get_root_project_path(config_file_path=None):
 
112
    """Try to guess where the .quickly config file is.
 
113
 
 
114
    config_file_path is optional (needed by the create command, for instance).
 
115
    getcwd() is taken by default.
 
116
    If nothing found, try to find it up to 6 parent directory
 
117
 
 
118
    :return project_path. Raise a project_path_not_found elsewhere.
 
119
    """
 
120
 
 
121
    if config_file_path is None:
 
122
        current_path = os.getcwd()
 
123
    else:
 
124
        current_path = config_file_path
 
125
 
 
126
    for related_directory in ('./', './', '../', '../../',
 
127
                              '../../../', '../../../../',
 
128
                              '../../../../../', '../../../../../../'):
 
129
        quickly_file_path = os.path.abspath(current_path + '/' + related_directory)
 
130
        quickly_file = quickly_file_path + "/.quickly"
 
131
        if os.path.isfile(quickly_file):
 
132
            return quickly_file_path
 
133
    raise project_path_not_found()
 
134
 
 
135
def apply_file_rights(src_file_name, dest_file_name):
 
136
    """Keep file rights from src to dest"""
 
137
    
 
138
    st = os.stat(src_file_name)
 
139
    mode = stat.S_IMODE(st.st_mode)
 
140
    os.chmod(dest_file_name, mode)
 
141