Skip to content

Commit 90cbb2f

Browse files
committed
Simplify ignores
1 parent 4fd2799 commit 90cbb2f

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

mbed/mbed.py

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def unignore(dest):
362362
@staticclass
363363
class Hg(object):
364364
name = 'hg'
365+
ignore_file = os.path.join('.hg', 'hgignore')
365366

366367
def isurl(url):
367368
m_url = re.match(regex_url_ref, url.strip().replace('\\', '/'))
@@ -476,7 +477,7 @@ def getbranch():
476477
def remoteid(url, rev=None):
477478
return pquery([hg_cmd, 'id', '--id', url] + (['-r', rev] if rev else [])).strip() or ""
478479

479-
def ignores():
480+
def hgrc():
480481
hook = 'ignore.local = .hg/hgignore'
481482
hgrc = os.path.join('.hg', 'hgrc')
482483
try:
@@ -493,68 +494,51 @@ def ignores():
493494
except IOError:
494495
error("Unable to write hgrc file in \"%s\"" % hgrc, 1)
495496

496-
exclude = os.path.join('.hg', 'hgignore')
497+
def ignores():
498+
Hg.hgrc()
497499
try:
498-
with open(exclude, 'w') as f:
500+
with open(Hg.ignore_file, 'w') as f:
499501
f.write("syntax: glob\n"+'\n'.join(ignores)+'\n')
500502
except IOError:
501-
error("Unable to write ignore file in \"%s\"" % exclude, 1)
503+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Hg.ignore_file), 1)
502504

503505
def ignore(dest):
504-
hook = 'ignore.local = .hg/hgignore'
505-
hgrc = os.path.join('.hg', 'hgrc')
506+
Hg.hgrc()
506507
try:
507-
with open(hgrc) as f:
508-
exists = hook in f.read().splitlines()
509-
except IOError:
510-
exists = False
511-
512-
if not exists:
513-
try:
514-
with open(hgrc, 'a') as f:
515-
f.write('[ui]\n')
516-
f.write(hook + '\n')
517-
except IOError:
518-
error("Unable to write hgrc file in \"%s\"" % hgrc, 1)
519-
520-
exclude = os.path.join('.hg', 'hgignore')
521-
try:
522-
with open(exclude) as f:
508+
with open(Hg.ignore_file) as f:
523509
exists = dest in f.read().splitlines()
524510
except IOError:
525511
exists = False
526512

527513
if not exists:
528514
try:
529-
with open(exclude, 'a') as f:
515+
with open(Hg.ignore_file, 'a') as f:
530516
f.write(dest + '\n')
531517
except IOError:
532-
error("Unable to write ignore file in \"%s\"" % exclude, 1)
518+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Hg.ignore_file), 1)
533519

534520
def unignore(dest):
535-
exclude = os.path.join('.hg', 'hgignore')
521+
Hg.ignore_file = os.path.join('.hg', 'hgignore')
536522
try:
537-
with open(exclude) as f:
523+
with open(Hg.ignore_file) as f:
538524
lines = f.read().splitlines()
539-
except:
525+
except IOError:
540526
lines = []
541527

542-
if dest not in lines:
543-
return
544-
545-
lines.remove(dest)
546-
547-
try:
548-
with open(exclude, 'w') as f:
549-
f.write('\n'.join(lines) + '\n')
550-
except IOError:
551-
error("Unable to write ignore file in \"%s\"" % exclude, 1)
528+
if dest in lines:
529+
lines.remove(dest)
530+
try:
531+
with open(Hg.ignore_file, 'w') as f:
532+
f.write('\n'.join(lines) + '\n')
533+
except IOError:
534+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Hg.ignore_file), 1)
552535

553536
# pylint: disable=no-self-argument, no-method-argument, no-member, no-self-use, unused-argument
554537
@scm('git')
555538
@staticclass
556539
class Git(object):
557540
name = 'git'
541+
ignore_file = os.path.join('.git', 'info', 'exclude')
558542

559543
def isurl(url):
560544
m_url = re.match(regex_url_ref, url.strip().replace('\\', '/'))
@@ -643,7 +627,7 @@ def update(repo, rev=None, clean=False):
643627
if clean:
644628
Git.discard(repo)
645629
if not repo.is_local:
646-
Git.fetch(repo)
630+
Git.fetch(repo)
647631
if rev:
648632
Git.checkout(repo, rev, clean)
649633
else:
@@ -755,36 +739,39 @@ def revbranches(rev):
755739
return branches
756740

757741
def ignores():
758-
with open(os.path.join('.git', 'info', 'exclude'), 'w') as f:
759-
f.write('\n'.join(ignores)+'\n')
742+
try:
743+
with open(Git.ignore_file, 'w') as f:
744+
f.write('\n'.join(ignores)+'\n')
745+
except IOError:
746+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Git.ignore_file), 1)
760747

761748
def ignore(dest):
762-
exclude = os.path.join('.git', 'info', 'exclude')
763749
try:
764-
with open(exclude) as f:
750+
with open(Git.ignore_file) as f:
765751
exists = dest in f.read().splitlines()
766752
except IOError:
767753
exists = False
768754

769755
if not exists:
770-
with open(exclude, 'a') as f:
771-
f.write(dest.replace("\\", "/") + '\n')
772-
756+
try:
757+
with open(Git.ignore_file, 'a') as f:
758+
f.write(dest.replace("\\", "/") + '\n')
759+
except IOError:
760+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Git.ignore_file), 1)
773761
def unignore(dest):
774-
exclude = os.path.join('.git', 'info', 'exclude')
775762
try:
776-
with open(exclude) as f:
763+
with open(Git.ignore_file) as f:
777764
lines = f.read().splitlines()
778-
except:
765+
except IOError:
779766
lines = []
780767

781-
if dest not in lines:
782-
return
783-
lines.remove(dest)
784-
785-
with open(exclude, 'w') as f:
786-
f.write('\n'.join(lines) + '\n')
787-
768+
if dest in lines:
769+
lines.remove(dest)
770+
try:
771+
with open(Git.ignore_file, 'w') as f:
772+
f.write('\n'.join(lines) + '\n')
773+
except IOError:
774+
error("Unable to write ignore file in \"%s\"" % os.path.join(os.getcwd, Git.ignore_file), 1)
788775

789776
# Repository object
790777
class Repo(object):

0 commit comments

Comments
 (0)