~ubuntu-branches/debian/stretch/apt-venv/stretch

« back to all changes in this revision

Viewing changes to apt_venv/utils.py

  • Committer: Package Import Robot
  • Author(s): Leo Iannacone
  • Date: 2014-05-05 11:49:30 UTC
  • Revision ID: package-import@ubuntu.com-20140505114930-xelqhp09gh5cj8fi
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from apt_venv import templates
 
2
import codecs
 
3
import os
 
4
 
 
5
DEBUG_LEVEL = 0
 
6
 
 
7
def debug(level, msg):
 
8
    if level <= DEBUG_LEVEL:
 
9
        print(" debug [%s] - %s" % (level, msg))
 
10
 
 
11
def get_template(filename):
 
12
    result = None
 
13
    if filename == 'sources.list_ubuntu':
 
14
        result = templates.SOURCES_LIST['ubuntu']
 
15
    elif filename == 'sources.list_debian':
 
16
        result = templates.SOURCES_LIST['debian']
 
17
    elif filename == 'bash.rc':
 
18
        result = templates.BASHRC
 
19
    elif filename == 'apt.conf':
 
20
        result = templates.APT_CONF
 
21
    elif filename == 'FAKE_SU':
 
22
        return templates.FAKE_SU
 
23
    return result
 
24
 
 
25
def create_file(filename, content):
 
26
    if not os.path.isfile(filename):
 
27
        debug(2, "creating file %s" % filename)
 
28
        content = u'%s' % content
 
29
        if not content or content[-1] != '\n':
 
30
            content += '\n'
 
31
        with codecs.open(filename, 'w', 'utf-8') as writer:
 
32
            writer.write(content)
 
33
 
 
34
def touch_file(filename):
 
35
    if not os.path.isfile(filename):
 
36
        debug(2, "touching file %s" % filename)
 
37
        open(filename, 'a').close()
 
38
 
 
39
def create_dir(directory):
 
40
    if not os.path.isdir(directory):
 
41
        debug(2, "creating directory %s" % directory)
 
42
        os.makedirs(directory)
 
43
 
 
44
def create_symlink(orig, dest):
 
45
    if os.path.exists(orig) and not os.path.lexists(dest):
 
46
        debug(2, "creating symlink %s -> %s" % (orig, dest))
 
47
        os.symlink(orig, dest)