"""
import logging as _logging
+import os.path as _os_path
from update_copyright import LOG as _LOG
from update_copyright.project import Project
_LOG.setLevel(max(_logging.DEBUG, _logging.ERROR - 10*options.verbose))
- project = Project()
+ project = Project(root=_os_path.dirname(_os_path.abspath(options.config)))
project.load_config(open(options.config, 'r'))
if options.authors:
project.update_authors(dry_run=options.dry_run)
class Project (object):
- def __init__(self, name=None, vcs=None, copyright=None,
+ def __init__(self, root='.', name=None, vcs=None, copyright=None,
short_copyright=None):
+ self._root = _os_path.normpath(_os_path.abspath(root))
self._name = name
self._vcs = vcs
self._author_hacks = None
pass
else:
kwargs = {
+ 'root': self._root,
'author_hacks': self._author_hacks,
'year_hacks': self._year_hacks,
'aliases': self._aliases,
else:
self._ignored_paths = [pth.strip() for pth in ignored.split(',')]
try:
- self._pyfile = parser.get('files', 'pyfile')
+ pyfile = parser.get('files', 'pyfile')
except _configparser.NoOptionError:
pass
+ else:
+ self._pyfile = _os_path.join(self._root, pyfile)
def _load_author_hacks_conf(self, parser, encoding=None):
if encoding is None:
new_contents = u'{} was written by:\n{}\n'.format(
self._name, u'\n'.join(authors))
_utils.set_contents(
- 'AUTHORS', new_contents, unicode=True, encoding=self._encoding,
+ _os_path.join(self._root, 'AUTHORS'),
+ new_contents, unicode=True, encoding=self._encoding,
dry_run=dry_run)
def update_file(self, filename, dry_run=False):
def update_files(self, files=None, dry_run=False):
if files is None or len(files) == 0:
- files = _utils.list_files(root='.')
+ files = _utils.list_files(root=self._root)
for filename in files:
if self._ignored_file(filename=filename):
continue
>>> ignored_file('./z', ignored_paths, ignored_files, False, False)
False
"""
+ filename = _os_path.relpath(filename, self._root)
if self._ignored_paths is not None:
for path in self._ignored_paths:
if _fnmatch.fnmatch(filename, path):
"""Backends for version control systems."""
+import os.path as _os_path
+
from . import utils as _utils
class VCSBackend (object):
name = None
- def __init__(self, author_hacks=None, year_hacks=None, aliases=None):
+ def __init__(self, root='.', author_hacks=None, year_hacks=None,
+ aliases=None):
+ self._root = root
if author_hacks is None:
author_hacks = {}
self._author_hacks = author_hacks
years = self._years(filename=filename)
if filename is None:
years.update(self._year_hacks.values())
- elif _utils.splitpath(filename) in self._year_hacks:
- years.add(self._year_hacks[_utils.splitpath(filename)])
+ else:
+ filename = _os_path.relpath(filename, self._root)
+ if _utils.splitpath(filename) in self._year_hacks:
+ years.add(self._year_hacks[_utils.splitpath(filename)])
years = sorted(years)
return years[0]
# <http://www.gnu.org/licenses/>.
import StringIO as _StringIO
+import os as _os
import bzrlib as _bzrlib
import bzrlib.builtins as _bzrlib_builtins
super(BazaarBackend, self).__init__(**kwargs)
self._version = _bzrlib.__version__
+ def _bzr_cmd(self, cmd, **kwargs):
+ cwd = _os.getcwd()
+ _os.chdir(self._root)
+ try:
+ cmd.run(**kwargs)
+ finally:
+ _os.chdir(cwd)
+
def _years(self, filename=None):
cmd = _bzrlib_builtins.cmd_log()
cmd.outf = _StringIO.StringIO()
kwargs = {'log_format':_YearLogFormatter, 'levels':0}
if filename is not None:
kwargs['file_list'] = [filename]
- cmd.run(**kwargs)
+ self._bzr_cmd(cmd=cmd, **kwargs)
years = set(int(year) for year in cmd.outf.getvalue().splitlines())
return years
kwargs = {'log_format':_AuthorLogFormatter, 'levels':0}
if filename is not None:
kwargs['file_list'] = [filename]
- cmd.run(**kwargs)
+ self._bzr_cmd(cmd=cmd, **kwargs)
authors = set(cmd.outf.getvalue().splitlines())
return authors
def is_versioned(self, filename):
cmd = _bzrlib_builtins.cmd_log()
cmd.outf = StringIO.StringIO()
- cmd.run(file_list=[filename])
+ self._bzr_cmd(cmd=cmd, file_list=[filename])
return True
class GitBackend (_VCSBackend):
name = 'Git'
- @staticmethod
- def _git_cmd(*args):
- status,stdout,stderr = _utils.invoke(
- ['git'] + list(args), unicode_output=True)
- return stdout.rstrip('\n')
-
def __init__(self, **kwargs):
super(GitBackend, self).__init__(**kwargs)
self._version = self._git_cmd('--version').split(' ')[-1]
self._year_format = ['--pretty=format:%ad', # Author date
'--date=short'] # YYYY-MM-DD
+ def _git_cmd(self, *args):
+ status,stdout,stderr = _utils.invoke(
+ ['git'] + list(args), cwd=self._root, unicode_output=True)
+ return stdout.rstrip('\n')
+
def _years(self, filename=None):
args = ['log'] + self._year_format
if filename is not None:
class MercurialBackend (_VCSBackend):
name = 'Mercurial'
- @staticmethod
+ def __init__(self, **kwargs):
+ super(MercurialBackend, self).__init__(**kwargs)
+ self._version = _version
+
def _hg_cmd(*args):
cwd = _os.getcwd()
stdout = _sys.stdout
tmp_stderr = _StringIO.StringIO()
_sys.stdout = tmp_stdout
_sys.stderr = tmp_stderr
+ _os.chdir(self._root)
try:
_mercurial_dispatch.dispatch(list(args))
finally:
return (tmp_stdout.getvalue().rstrip('\n'),
tmp_stderr.getvalue().rstrip('\n'))
- def __init__(self, **kwargs):
- super(MercurialBackend, self).__init__(**kwargs)
- self._version = _version
-
def _years(self, filename=None):
args = [
'--template', '{date|shortdate}\n',
def invoke(args, stdin=None, stdout=_subprocess.PIPE, stderr=_subprocess.PIPE,
- expect=(0,), unicode_output=False, encoding=None):
+ cwd=None, expect=(0,), unicode_output=False, encoding=None):
"""Invoke an external program and return the results
``expect`` should be a tuple of allowed exit codes.
try :
if _POSIX:
q = _subprocess.Popen(args, stdin=_subprocess.PIPE,
- stdout=stdout, stderr=stderr)
+ stdout=stdout, stderr=stderr,
+ close_fds=True, cwd=cwd)
else:
assert _MSWINDOWS == True, 'invalid platform'
# win32 don't have os.execvp() so run the command in a shell
q = _subprocess.Popen(args, stdin=_subprocess.PIPE,
- stdout=stdout, stderr=stderr, shell=True)
+ stdout=stdout, stderr=stderr, shell=True,
+ cwd=cwd)
except OSError, e:
raise ValueError([args, e])
stdout,stderr = q.communicate(input=stdin)
while True:
dirname,basename = _os_path.split(path)
elements.insert(0,basename)
- if dirname in ['', '.']:
+ if dirname in ['/', '', '.']:
break
path = dirname
return tuple(elements)