From aca25db85da76eb7edec95eb9660a467a6fc49a9 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 Dec 2018 12:38:41 +0500 Subject: [PATCH 1/3] Python 3 --- .gitignore | 2 ++ gitosis/access.py | 2 +- gitosis/app.py | 14 +++++++------- gitosis/gitdaemon.py | 6 +++--- gitosis/gitweb.py | 6 +++--- gitosis/group.py | 2 +- gitosis/init.py | 12 ++++++------ gitosis/repository.py | 4 ++-- gitosis/run_hook.py | 4 ++-- gitosis/serve.py | 6 +++--- gitosis/ssh.py | 8 ++++---- gitosis/util.py | 4 ++-- 12 files changed, 36 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index dc2c237..9593815 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /apidocs /gitosis/test/tmp /.coverage +.idea +venv \ No newline at end of file diff --git a/gitosis/access.py b/gitosis/access.py index c95c842..f7153b2 100644 --- a/gitosis/access.py +++ b/gitosis/access.py @@ -1,5 +1,5 @@ import os, logging -from ConfigParser import NoSectionError, NoOptionError +from configparser import NoSectionError, NoOptionError from gitosis import group diff --git a/gitosis/app.py b/gitosis/app.py index fa9772b..88106c9 100644 --- a/gitosis/app.py +++ b/gitosis/app.py @@ -3,7 +3,7 @@ import sys import logging import optparse import errno -import ConfigParser +import configparser log = logging.getLogger('gitosis.app') @@ -31,7 +31,7 @@ class App(object): cfg = self.create_config(options) try: self.read_config(options, cfg) - except CannotReadConfigError, e: + except CannotReadConfigError as e: log.error(str(e)) sys.exit(1) self.setup_logging(cfg) @@ -53,13 +53,13 @@ class App(object): return parser def create_config(self, options): - cfg = ConfigParser.RawConfigParser() + cfg = configparser.RawConfigParser() return cfg def read_config(self, options, cfg): try: - conffile = file(options.config) - except (IOError, OSError), e: + conffile = open(options.config) + except (IOError, OSError) as e: if e.errno == errno.ENOENT: # special case this because gitosis-init wants to # ignore this particular error case @@ -74,8 +74,8 @@ class App(object): def setup_logging(self, cfg): try: loglevel = cfg.get('gitosis', 'loglevel') - except (ConfigParser.NoSectionError, - ConfigParser.NoOptionError): + except (configparser.NoSectionError, + configparser.NoOptionError): pass else: try: diff --git a/gitosis/gitdaemon.py b/gitosis/gitdaemon.py index 78ca9ea..f2b55c5 100644 --- a/gitosis/gitdaemon.py +++ b/gitosis/gitdaemon.py @@ -2,7 +2,7 @@ import errno import logging import os -from ConfigParser import NoSectionError, NoOptionError +from configparser import NoSectionError, NoOptionError log = logging.getLogger('gitosis.gitdaemon') @@ -14,13 +14,13 @@ def export_ok_path(repopath): def allow_export(repopath): p = export_ok_path(repopath) - file(p, 'a').close() + open(p, 'a').close() def deny_export(repopath): p = export_ok_path(repopath) try: os.unlink(p) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: pass else: diff --git a/gitosis/gitweb.py b/gitosis/gitweb.py index b4b538b..6d1cc67 100644 --- a/gitosis/gitweb.py +++ b/gitosis/gitweb.py @@ -27,7 +27,7 @@ To plug this into ``gitweb``, you have two choices. import os, urllib, logging -from ConfigParser import NoSectionError, NoOptionError +from configparser import NoSectionError, NoOptionError from gitosis import util @@ -106,7 +106,7 @@ def generate_project_list(config, path): """ tmp = '%s.%d.tmp' % (path, os.getpid()) - f = file(tmp, 'w') + f = open(tmp, 'w') try: generate_project_list_fp(config=config, fp=f) finally: @@ -157,7 +157,7 @@ def set_descriptions(config): 'description', ) tmp = '%s.%d.tmp' % (path, os.getpid()) - f = file(tmp, 'w') + f = open(tmp, 'w') try: print >>f, description finally: diff --git a/gitosis/group.py b/gitosis/group.py index a18a731..0a2b010 100644 --- a/gitosis/group.py +++ b/gitosis/group.py @@ -1,5 +1,5 @@ import logging -from ConfigParser import NoSectionError, NoOptionError +from configparser import NoSectionError, NoOptionError def _getMembership(config, user, seen): log = logging.getLogger('gitosis.group.getMembership') diff --git a/gitosis/init.py b/gitosis/init.py index 28e7871..214b35c 100644 --- a/gitosis/init.py +++ b/gitosis/init.py @@ -8,8 +8,8 @@ import os import sys from pkg_resources import resource_filename -from cStringIO import StringIO -from ConfigParser import RawConfigParser +from io import StringIO +from configparser import RawConfigParser from gitosis import repository from gitosis import run_hook @@ -54,7 +54,7 @@ def symlink_config(git_dir): tmp = '%s.%d.tmp' % (dst, os.getpid()) try: os.unlink(tmp) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: pass else: @@ -80,7 +80,7 @@ def init_admin_repository( # can't rely on setuptools and all kinds of distro packaging to # have kept our templates executable, it seems - os.chmod(os.path.join(git_dir, 'hooks', 'post-update'), 0755) + os.chmod(os.path.join(git_dir, 'hooks', 'post-update'), 0o755) if not repository.has_initial_commit(git_dir): log.info('Making initial commit...') @@ -119,7 +119,7 @@ class Main(app.App): def handle_args(self, parser, cfg, options, args): super(Main, self).handle_args(parser, cfg, options, args) - os.umask(0022) + os.umask(0o022) log.info('Reading SSH public key...') pubkey = read_ssh_pubkey() @@ -141,7 +141,7 @@ class Main(app.App): user=user, ) log.info('Running post-update hook...') - util.mkdir(os.path.expanduser('~/.ssh'), 0700) + util.mkdir(os.path.expanduser('~/.ssh'), 0o700) run_hook.post_update(cfg=cfg, git_dir=admin_repository) log.info('Symlinking ~/.gitosis.conf to repository...') symlink_config(git_dir=admin_repository) diff --git a/gitosis/repository.py b/gitosis/repository.py index 9dd0291..8617afe 100644 --- a/gitosis/repository.py +++ b/gitosis/repository.py @@ -36,7 +36,7 @@ def init( if _git is None: _git = 'git' - util.mkdir(path, 0750) + util.mkdir(path, 0o750) args = [ _git, '--git-dir=.', @@ -131,7 +131,7 @@ class GitCheckoutIndexError(GitExportError): def export(git_dir, path): try: os.mkdir(path) - except OSError, e: + except OSError as e: if e.errno == errno.EEXIST: pass else: diff --git a/gitosis/run_hook.py b/gitosis/run_hook.py index e535e6a..ca35801 100644 --- a/gitosis/run_hook.py +++ b/gitosis/run_hook.py @@ -19,7 +19,7 @@ def post_update(cfg, git_dir): export = os.path.join(git_dir, 'gitosis-export') try: shutil.rmtree(export) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: pass else: @@ -63,7 +63,7 @@ class Main(app.App): parser.error('Missing argument HOOK.') log = logging.getLogger('gitosis.run_hook') - os.umask(0022) + os.umask(0o022) git_dir = os.environ.get('GIT_DIR') if git_dir is None: diff --git a/gitosis/serve.py b/gitosis/serve.py index fdfea53..3b69f7a 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -141,7 +141,7 @@ def serve( p = topdir for segment in repopath.split(os.sep)[:-1]: p = os.path.join(p, segment) - util.mkdir(p, 0750) + util.mkdir(p, 0o750) repository.init(path=fullpath) gitweb.set_descriptions( @@ -178,7 +178,7 @@ class Main(app.App): parser.error('Missing argument USER.') main_log = logging.getLogger('gitosis.serve.main') - os.umask(0022) + os.umask(0o022) cmd = os.environ.get('SSH_ORIGINAL_COMMAND', None) if cmd is None: @@ -197,7 +197,7 @@ class Main(app.App): user=user, command=cmd, ) - except ServingError, e: + except ServingError as e: main_log.error('%s', e) sys.exit(1) diff --git a/gitosis/ssh.py b/gitosis/ssh.py index a315a5c..37c9fec 100644 --- a/gitosis/ssh.py +++ b/gitosis/ssh.py @@ -25,7 +25,7 @@ def readKeys(keydir): continue path = os.path.join(keydir, filename) - f = file(path) + f = open(path) for line in f: line = line.rstrip('\n') yield (basename, line) @@ -63,15 +63,15 @@ def filterAuthorizedKeys(fp): def writeAuthorizedKeys(path, keydir): tmp = '%s.%d.tmp' % (path, os.getpid()) try: - in_ = file(path) - except IOError, e: + in_ = open(path) + except IOError as e: if e.errno == errno.ENOENT: in_ = None else: raise try: - out = file(tmp, 'w') + out = open(tmp, 'w') try: if in_ is not None: for line in filterAuthorizedKeys(in_): diff --git a/gitosis/util.py b/gitosis/util.py index 479b2e9..f4bf05f 100644 --- a/gitosis/util.py +++ b/gitosis/util.py @@ -1,11 +1,11 @@ import errno import os -from ConfigParser import NoSectionError, NoOptionError +from configparser import NoSectionError, NoOptionError def mkdir(*a, **kw): try: os.mkdir(*a, **kw) - except OSError, e: + except OSError as e: if e.errno == errno.EEXIST: pass else: From 35a9a8a8babe5a9f395ad3003cf0dcab5ed1e80c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 17 Dec 2018 00:38:12 +0500 Subject: [PATCH 2/3] Python3 --- gitosis/test/test_access.py | 2 +- gitosis/test/test_gitdaemon.py | 2 +- gitosis/test/test_gitweb.py | 4 ++-- gitosis/test/test_group.py | 2 +- gitosis/test/test_init.py | 4 ++-- gitosis/test/test_repository.py | 22 +++++++++++----------- gitosis/test/test_run_hook.py | 4 ++-- gitosis/test/test_serve.py | 10 +++++----- gitosis/test/test_ssh.py | 4 ++-- gitosis/test/util.py | 10 +++++----- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/gitosis/test/test_access.py b/gitosis/test/test_access.py index f39444c..a65742b 100644 --- a/gitosis/test/test_access.py +++ b/gitosis/test/test_access.py @@ -1,7 +1,7 @@ from nose.tools import eq_ as eq import logging -from ConfigParser import RawConfigParser +from configparser import RawConfigParser from gitosis import access diff --git a/gitosis/test/test_gitdaemon.py b/gitosis/test/test_gitdaemon.py index 94475ac..d9547a6 100644 --- a/gitosis/test/test_gitdaemon.py +++ b/gitosis/test/test_gitdaemon.py @@ -1,7 +1,7 @@ from nose.tools import eq_ as eq import os -from ConfigParser import RawConfigParser +from configparser import RawConfigParser from gitosis import gitdaemon from gitosis.test.util import maketemp, writeFile diff --git a/gitosis/test/test_gitweb.py b/gitosis/test/test_gitweb.py index e38b881..4b35962 100644 --- a/gitosis/test/test_gitweb.py +++ b/gitosis/test/test_gitweb.py @@ -1,8 +1,8 @@ from nose.tools import eq_ as eq import os -from ConfigParser import RawConfigParser -from cStringIO import StringIO +from configparser import RawConfigParser +from io import StringIO from gitosis import gitweb from gitosis.test.util import mkdir, maketemp, readFile, writeFile diff --git a/gitosis/test/test_group.py b/gitosis/test/test_group.py index 9ea035a..4d878a8 100644 --- a/gitosis/test/test_group.py +++ b/gitosis/test/test_group.py @@ -1,6 +1,6 @@ from nose.tools import eq_ as eq, assert_raises -from ConfigParser import RawConfigParser +from configparser import RawConfigParser from gitosis import group diff --git a/gitosis/test/test_init.py b/gitosis/test/test_init.py index fb6b286..dcef74c 100644 --- a/gitosis/test/test_init.py +++ b/gitosis/test/test_init.py @@ -2,7 +2,7 @@ from nose.tools import eq_ as eq from gitosis.test.util import assert_raises, maketemp import os -from ConfigParser import RawConfigParser +from configparser import RawConfigParser from gitosis import init from gitosis import repository @@ -113,7 +113,7 @@ def test_init_admin_repository(): 'hooks', 'post-update', ) - util.check_mode(hook, 0755, is_file=True) + util.check_mode(hook, 0o755, is_file=True) got = util.readFile(hook).splitlines() assert 'gitosis-run-hook post-update' in got export_dir = os.path.join(tmp, 'export') diff --git a/gitosis/test/test_repository.py b/gitosis/test/test_repository.py index 1646e6c..9e5c7cf 100644 --- a/gitosis/test/test_repository.py +++ b/gitosis/test/test_repository.py @@ -23,17 +23,17 @@ def test_init_simple(): tmp = maketemp() path = os.path.join(tmp, 'repo.git') repository.init(path) - check_mode(path, 0750, is_dir=True) + check_mode(path, 0o750, is_dir=True) check_bare(path) def test_init_exist_dir(): tmp = maketemp() path = os.path.join(tmp, 'repo.git') - mkdir(path, 0710) - check_mode(path, 0710, is_dir=True) + mkdir(path, 0o710) + check_mode(path, 0o710, is_dir=True) repository.init(path) # my weird access mode is preserved - check_mode(path, 0710, is_dir=True) + check_mode(path, 0o710, is_dir=True) check_bare(path) def test_init_exist_git(): @@ -41,7 +41,7 @@ def test_init_exist_git(): path = os.path.join(tmp, 'repo.git') repository.init(path) repository.init(path) - check_mode(path, 0750, is_dir=True) + check_mode(path, 0o750, is_dir=True) check_bare(path) def test_init_templates(): @@ -53,7 +53,7 @@ def test_init_templates(): ) # for reproducibility - os.umask(0022) + os.umask(0o022) repository.init(path, template=templatedir) repository.init(path) @@ -61,7 +61,7 @@ def test_init_templates(): eq(got, 'i should show up\n') check_mode( os.path.join(path, 'hooks', 'post-update'), - 0755, + 0o755, is_file=True, ) got = readFile(os.path.join(path, 'hooks', 'post-update')) @@ -91,7 +91,7 @@ PATH="${PATH#*:}" exec git "$@" ''') - os.chmod(mockgit, 0755) + os.chmod(mockgit, 0o755) magic_cookie = '%d' % random.randint(1, 100000) good_path = os.environ['PATH'] try: @@ -130,7 +130,7 @@ PATH="${PATH#*:}" exec git "$@" ''') - os.chmod(mockgit, 0755) + os.chmod(mockgit, 0o755) magic_cookie = '%d' % random.randint(1, 100000) good_path = os.environ['PATH'] try: @@ -226,7 +226,7 @@ PATH="${PATH#*:}" exec git "$@" ''') - os.chmod(mockgit, 0755) + os.chmod(mockgit, 0o755) repository.init(path=git_dir) repository.fast_import( git_dir=git_dir, @@ -301,7 +301,7 @@ PATH="${PATH#*:}" exec git "$@" ''') - os.chmod(mockgit, 0755) + os.chmod(mockgit, 0o755) repository.init(path=tmp) repository.fast_import( git_dir=tmp, diff --git a/gitosis/test/test_run_hook.py b/gitosis/test/test_run_hook.py index db01e0c..ae674cc 100644 --- a/gitosis/test/test_run_hook.py +++ b/gitosis/test/test_run_hook.py @@ -1,8 +1,8 @@ from nose.tools import eq_ as eq import os -from ConfigParser import RawConfigParser -from cStringIO import StringIO +from configparser import RawConfigParser +from io import StringIO from gitosis import init, repository, run_hook from gitosis.test.util import maketemp, readFile diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index 88ce474..9d126c1 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -3,8 +3,8 @@ from gitosis.test.util import assert_raises import logging import os -from cStringIO import StringIO -from ConfigParser import RawConfigParser +from io import StringIO +from configparser import RawConfigParser from gitosis import serve from gitosis import repository @@ -354,7 +354,7 @@ def test_push_inits_subdir_parent_missing(): ) eq(os.listdir(repositories), ['foo']) foo = os.path.join(repositories, 'foo') - util.check_mode(foo, 0750, is_dir=True) + util.check_mode(foo, 0o750, is_dir=True) eq(os.listdir(foo), ['bar.git']) assert os.path.isfile(os.path.join(repositories, 'foo', 'bar.git', 'HEAD')) @@ -366,7 +366,7 @@ def test_push_inits_subdir_parent_exists(): os.mkdir(repositories) foo = os.path.join(repositories, 'foo') # silly mode on purpose; not to be touched - os.mkdir(foo, 0751) + os.mkdir(foo, 0o751) cfg.set('gitosis', 'repositories', repositories) generated = os.path.join(tmp, 'generated') os.mkdir(generated) @@ -380,7 +380,7 @@ def test_push_inits_subdir_parent_exists(): command="git-receive-pack 'foo/bar.git'", ) eq(os.listdir(repositories), ['foo']) - util.check_mode(foo, 0751, is_dir=True) + util.check_mode(foo, 0o751, is_dir=True) eq(os.listdir(foo), ['bar.git']) assert os.path.isfile(os.path.join(repositories, 'foo', 'bar.git', 'HEAD')) diff --git a/gitosis/test/test_ssh.py b/gitosis/test/test_ssh.py index fc6ecbc..8714684 100644 --- a/gitosis/test/test_ssh.py +++ b/gitosis/test/test_ssh.py @@ -1,7 +1,7 @@ from nose.tools import eq_ as eq, assert_raises import os -from cStringIO import StringIO +from io import StringIO from gitosis import ssh from gitosis.test.util import mkdir, maketemp, writeFile, readFile @@ -171,7 +171,7 @@ class WriteAuthorizedKeys_Test(object): def test_simple(self): tmp = maketemp() path = os.path.join(tmp, 'authorized_keys') - f = file(path, 'w') + f = open(path, 'w') try: f.write('''\ # foo diff --git a/gitosis/test/util.py b/gitosis/test/util.py index 592b766..6b04c9a 100644 --- a/gitosis/test/util.py +++ b/gitosis/test/util.py @@ -9,7 +9,7 @@ import sys def mkdir(*a, **kw): try: os.mkdir(*a, **kw) - except OSError, e: + except OSError as e: if e.errno == errno.EEXIST: pass else: @@ -27,7 +27,7 @@ def maketemp(): tmp = os.path.join(tmp, name) try: shutil.rmtree(tmp) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: pass else: @@ -37,7 +37,7 @@ def maketemp(): def writeFile(path, content): tmp = '%s.tmp' % path - f = file(tmp, 'w') + f = open(tmp, 'w') try: f.write(content) finally: @@ -45,7 +45,7 @@ def writeFile(path, content): os.rename(tmp, path) def readFile(path): - f = file(path) + f = open(path) try: data = f.read() finally: @@ -58,7 +58,7 @@ def assert_raises(excClass, callableObj, *args, **kwargs): """ try: callableObj(*args, **kwargs) - except excClass, e: + except excClass as e: return e else: if hasattr(excClass,'__name__'): excName = excClass.__name__ From a5c8c2022b79f406fc4ce30d78c0ad16d2619f97 Mon Sep 17 00:00:00 2001 From: Michael Gukov Date: Mon, 17 Dec 2018 01:47:01 +0500 Subject: [PATCH 3/3] Python3 --- gitosis/init.py | 7 +++++-- gitosis/repository.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/gitosis/init.py b/gitosis/init.py index 214b35c..d81ceed 100644 --- a/gitosis/init.py +++ b/gitosis/init.py @@ -87,8 +87,11 @@ def init_admin_repository( # ConfigParser does not guarantee order, so jump through hoops # to make sure [gitosis] is first cfg_file = StringIO() - print >>cfg_file, '[gitosis]' - print >>cfg_file + print('[gitosis]', file=cfg_file) + #print('', end="", file=cfg_file) + + #print >>cfg_file, '[gitosis]' + #print >>cfg_file cfg = RawConfigParser() cfg.add_section('group gitosis-admin') cfg.set('group gitosis-admin', 'members', user) diff --git a/gitosis/repository.py b/gitosis/repository.py index 8617afe..884549b 100644 --- a/gitosis/repository.py +++ b/gitosis/repository.py @@ -185,7 +185,7 @@ def has_initial_commit(git_dir): stdout=subprocess.PIPE, close_fds=True, ) - got = child.stdout.read() + got = child.stdout.read().decode('utf-8') returncode = child.wait() if returncode != 0: raise GitRevParseError('exit status %d' % returncode)