/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Jan Balster
  • Date: 2006-08-15 12:39:42 UTC
  • mfrom: (1923 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1928.
  • Revision ID: jan@merlinux.de-20060815123942-22c388c6e9a8ac91
merge bzr.dev 1923

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
create_signatures - this option controls whether bzr will always create 
50
50
                    gpg signatures, never create them, or create them if the
51
51
                    branch is configured to require them.
52
 
log_format - This options set the default log format.  Options are long, 
53
 
             short, line, or a plugin can register new formats
 
52
log_format - this option sets the default log format.  Possible values are
 
53
             long, short, line, or a plugin can register new formats.
54
54
 
55
55
In bazaar.conf you can also define aliases in the ALIASES sections, example
56
56
 
70
70
from StringIO import StringIO
71
71
 
72
72
import bzrlib
73
 
import bzrlib.errors as errors
 
73
from bzrlib import errors, urlutils
74
74
from bzrlib.osutils import pathjoin
75
75
from bzrlib.trace import mutter, warning
76
76
import bzrlib.util.configobj.configobj as configobj
167
167
    
168
168
        Something similar to 'Martin Pool <mbp@sourcefrog.net>'
169
169
        
170
 
        $BZREMAIL can be set to override this, then
 
170
        $BZR_EMAIL can be set to override this (as well as the
 
171
        deprecated $BZREMAIL), then
171
172
        the concrete policy type is checked, and finally
172
173
        $EMAIL is examined.
173
174
        If none is found, a reasonable default is (hopefully)
175
176
    
176
177
        TODO: Check it's reasonably well-formed.
177
178
        """
 
179
        v = os.environ.get('BZR_EMAIL')
 
180
        if v:
 
181
            return v.decode(bzrlib.user_encoding)
178
182
        v = os.environ.get('BZREMAIL')
179
183
        if v:
 
184
            warning('BZREMAIL is deprecated in favor of BZR_EMAIL. Please update your configuration.')
180
185
            return v.decode(bzrlib.user_encoding)
181
186
    
182
187
        v = self._get_user_id()
337
342
    def __init__(self):
338
343
        super(GlobalConfig, self).__init__(config_filename)
339
344
 
 
345
    def set_user_option(self, option, value):
 
346
        """Save option and its value in the configuration."""
 
347
        # FIXME: RBC 20051029 This should refresh the parser and also take a
 
348
        # file lock on bazaar.conf.
 
349
        conf_dir = os.path.dirname(self._get_filename())
 
350
        ensure_config_dir_exists(conf_dir)
 
351
        if 'DEFAULT' not in self._get_parser():
 
352
            self._get_parser()['DEFAULT'] = {}
 
353
        self._get_parser()['DEFAULT'][option] = value
 
354
        f = open(self._get_filename(), 'wb')
 
355
        self._get_parser().write(f)
 
356
        f.close()
 
357
 
340
358
 
341
359
class LocationConfig(IniBasedConfig):
342
360
    """A configuration object that gives the policy for a location."""
345
363
        name_generator = locations_config_filename
346
364
        if (not os.path.exists(name_generator()) and 
347
365
                os.path.exists(branches_config_filename())):
348
 
            warning('Please rename branches.conf to locations.conf')
 
366
            if sys.platform == 'win32':
 
367
                warning('Please rename %s to %s' 
 
368
                         % (branches_config_filename(),
 
369
                            locations_config_filename()))
 
370
            else:
 
371
                warning('Please rename ~/.bazaar/branches.conf'
 
372
                        ' to ~/.bazaar/locations.conf')
349
373
            name_generator = branches_config_filename
350
374
        super(LocationConfig, self).__init__(name_generator)
 
375
        # local file locations are looked up by local path, rather than
 
376
        # by file url. This is because the config file is a user
 
377
        # file, and we would rather not expose the user to file urls.
 
378
        if location.startswith('file://'):
 
379
            location = urlutils.local_path_from_url(location)
351
380
        self.location = location
352
381
 
353
382
    def _get_section(self):
363
392
            del location_names[-1]
364
393
        matches=[]
365
394
        for section in sections:
366
 
            section_names = section.split('/')
 
395
            # location is a local path if possible, so we need
 
396
            # to convert 'file://' urls to local paths if necessary.
 
397
            # This also avoids having file:///path be a more exact
 
398
            # match than '/path'.
 
399
            if section.startswith('file://'):
 
400
                section_path = urlutils.local_path_from_url(section)
 
401
            else:
 
402
                section_path = section
 
403
            section_names = section_path.split('/')
367
404
            if section.endswith('/'):
368
405
                del section_names[-1]
369
406
            names = zip(location_names, section_names)
512
549
        return self._get_safe_value('_post_commit')
513
550
 
514
551
    def _get_nickname(self):
515
 
        value = self._get_best_value('_get_nickname')
 
552
        value = self._get_explicit_nickname()
516
553
        if value is not None:
517
554
            return value
518
555
        return self.branch.base.split('/')[-2]
519
556
 
 
557
    def has_explicit_nickname(self):
 
558
        """Return true if a nickname has been explicitly assigned."""
 
559
        return self._get_explicit_nickname() is not None
 
560
 
 
561
    def _get_explicit_nickname(self):
 
562
        return self._get_best_value('_get_nickname')
 
563
 
520
564
    def _log_format(self):
521
565
        """See Config.log_format."""
522
566
        return self._get_best_value('_log_format')
572
616
    """Return per-user configuration ini file filename."""
573
617
    return pathjoin(config_dir(), 'branches.conf')
574
618
 
 
619
 
575
620
def locations_config_filename():
576
621
    """Return per-user configuration ini file filename."""
577
622
    return pathjoin(config_dir(), 'locations.conf')
578
623
 
579
624
 
 
625
def user_ignore_config_filename():
 
626
    """Return the user default ignore filename"""
 
627
    return pathjoin(config_dir(), 'ignore')
 
628
 
 
629
 
580
630
def _auto_user_id():
581
631
    """Calculate automatic user identification.
582
632
 
597
647
        uid = os.getuid()
598
648
        w = pwd.getpwuid(uid)
599
649
 
600
 
        try:
601
 
            gecos = w.pw_gecos.decode(bzrlib.user_encoding)
602
 
            username = w.pw_name.decode(bzrlib.user_encoding)
603
 
        except UnicodeDecodeError:
604
 
            # We're using pwd, therefore we're on Unix, so /etc/passwd is ok.
605
 
            raise errors.BzrError("Can't decode username in " \
606
 
                    "/etc/passwd as %s." % bzrlib.user_encoding)
 
650
        # we try utf-8 first, because on many variants (like Linux),
 
651
        # /etc/passwd "should" be in utf-8, and because it's unlikely to give
 
652
        # false positives.  (many users will have their user encoding set to
 
653
        # latin-1, which cannot raise UnicodeError.)
 
654
        try:
 
655
            gecos = w.pw_gecos.decode('utf-8')
 
656
            encoding = 'utf-8'
 
657
        except UnicodeError:
 
658
            try:
 
659
                gecos = w.pw_gecos.decode(bzrlib.user_encoding)
 
660
                encoding = bzrlib.user_encoding
 
661
            except UnicodeError:
 
662
                raise errors.BzrCommandError('Unable to determine your name.  '
 
663
                   'Use "bzr whoami" to set it.')
 
664
        try:
 
665
            username = w.pw_name.decode(encoding)
 
666
        except UnicodeError:
 
667
            raise errors.BzrCommandError('Unable to determine your name.  '
 
668
                'Use "bzr whoami" to set it.')
607
669
 
608
670
        comma = gecos.find(',')
609
671
        if comma == -1: