Skip to content

Commit

Permalink
Support mepo dir in parent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
pchakraborty committed Oct 28, 2019
1 parent 4bae134 commit 30ad1cb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 66 deletions.
58 changes: 0 additions & 58 deletions mepo.d/Mepo.py

This file was deleted.

4 changes: 2 additions & 2 deletions mepo.d/checkout/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import subprocess as sp

from Mepo import MepoState
from mepo_state import MepoState

def run(args):
MepoState.initialize(args.cf)
Expand All @@ -30,6 +30,6 @@ def __checkout_components(repo):

def __git_clone(url, branch_or_tag, local_path):
cmd = 'git clone -b %s %s %s' % (branch_or_tag, url, local_path)
output_file = os.path.join(MepoState.mepo_state_dir, 'checkout.log')
output_file = os.path.join(MepoState.get_dir(), 'checkout.log')
with open(output_file, 'a') as fnull:
sp.check_call(cmd.split(), stderr=fnull)
80 changes: 80 additions & 0 deletions mepo.d/mepo_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import sys
import csv
import json

from collections import OrderedDict

def get_parent_dirs():
mypath = os.getcwd()
parentdirs = [mypath]
while mypath != '/':
mypath = os.path.dirname(mypath)
parentdirs.append(mypath)
return parentdirs

def write_state(repolist, csv_writer):
myrepos = repolist['Components']
for reponame in myrepos:
repo = myrepos[reponame]
remote = repo['remote']
branch = repo.get('branch') # d.get(key) is None if key is not present
tag = repo.get('tag')
# TODO: Can't have both branch and tag
local_path = os.path.join(os.getcwd(), repo['local'])
csv_writer.writerow([reponame, remote, tag, branch, local_path])
if 'Components' in repo:
write_state(repo, csv_writer) # recurse

class MepoState(object):

__dirname = '.mepo'
__filename = 'state.csv'

@classmethod
def get_dir(cls):
for mydir in get_parent_dirs():
mepo_dir = os.path.join(mydir, cls.__dirname)
if os.path.exists(mepo_dir):
return mepo_dir
raise OSError('mepo dir [.mepo] does not exist')

@classmethod
def get_file(cls):
mepo_file = os.path.join(cls.get_dir(), cls.__filename)
if os.path.exists(mepo_file):
return mepo_file
raise OSError('mepo file [%s] does not exist' % mepo_file)

@classmethod
def exists(cls):
try:
cls.get_file()
return True
except OSError:
return False

@classmethod
def initialize(cls, project_config_file):
if cls.exists():
sys.exit('ERROR: mepo state already exists')
new_mepo_dir = os.path.join(os.getcwd(), cls.__dirname)
new_mepo_file = os.path.join(new_mepo_dir, cls.__filename)
os.mkdir(new_mepo_dir)
with open(project_config_file, 'r') as fin:
repolist = json.load(fin, object_pairs_hook=OrderedDict)
with open(new_mepo_file, 'w') as fout:
csv_writer = csv.writer(fout, delimiter = ',', quotechar = '"')
csv_writer.writerow(['name', 'origin', 'tag', 'branch', 'path'])
write_state(repolist, csv_writer)

@classmethod
def read_state(cls):
if not cls.exists():
sys.exit('ERROR: mepo state does not exist')
allrepos = []
with open(cls.get_file(), 'r') as fin:
reader = csv.DictReader(fin, delimiter = ',')
for row in reader:
allrepos.append(row)
return allrepos
14 changes: 8 additions & 6 deletions mepo.d/status/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import subprocess as sp

from Mepo import MepoState
from mepo_state import MepoState

def run(args):
allrepos = MepoState.read_state()
Expand All @@ -14,12 +14,14 @@ def __check_status(repo, verbose=False):
orig_branch_or_tag = repo['branch'] or repo['tag']
os.chdir(repo['path'])
output = sp.check_output('git status -s'.split())
changes = ''
print('{:<14.14s} | {:<40.40s} | {:<33s}'.
format(
repo['name'],
os.path.relpath(repo['path'], cwd),
__get_current_branch_or_tag(repo['path'])))
if (output):
changes = 'Y'
cur_branch_or_tag = __get_current_branch_or_tag(repo['path'])
print('{:<1s} | {:<14.14s} | {:<30.30s} | {:<30s}'.
format(changes, repo['name'], orig_branch_or_tag, cur_branch_or_tag))
for line in output.split('\n'):
print ' |', line.rstrip()
os.chdir(cwd)

def __get_current_branch_or_tag(repo_path):
Expand Down

0 comments on commit 30ad1cb

Please sign in to comment.