Fixing Import Cycles.

Pulkit Goyal 7895pulkit at gmail.com
Tue Feb 23 21:11:35 EST 2016


Well doing this also resolves our next import cycle which was

Import cycle: hgext.largefiles.basestore -> hgext.largefiles.wirestore ->
hgext.largefiles.remotestore -> hgext.largefiles.basestore

The remotestore file require to import the basestore to use the same
StoreError class but since we have moved it in localstore file we can
easily remove that import cycle.

diff -r 2e11f6756d9c hgext/largefiles/remotestore.py
--- a/hgext/largefiles/remotestore.py    Tue Feb 16 14:49:29 2016 +0000
+++ b/hgext/largefiles/remotestore.py    Wed Feb 24 07:36:16 2016 +0530
@@ -12,7 +12,7 @@
 from mercurial.i18n import _

 import lfutil
-import basestore
+import localstore

 class remotestore(basestore.basestore):
     '''a largefile store accessed over a network'''
@@ -52,7 +52,7 @@
         except urllib2.HTTPError as e:
             # 401s get converted to error.Aborts; everything else is fine
being
             # turned into a StoreError
-            raise basestore.StoreError(filename, hash, self.url, str(e))
+            raise localstore.StoreError(filename, hash, self.url, str(e))
         except urllib2.URLError as e:
             # This usually indicates a connection problem, so don't
             # keep trying with the other files... they will probably

And something good pops out when I run
hg locate 'mercurial/**.py' 'hgext/**.py' | sed 's-\\-/-g' | python
"$import_checker" -
No more cyclic imports. Seriously. :)

Regards
Pulkit Goyal

On Wed, Feb 24, 2016 at 7:32 AM, Pulkit Goyal <7895pulkit at gmail.com> wrote:

> Hello Everyone,
>
> I was working on fixing import cycles in the codebase. The first import
> cycle was between hgext/largefiles/basestore and
> hgext/largefiles/localstore. The dependency can be solved by resolving the
> dependency of localstore on basestore. The dependency was of StoreError
> class which was required in the both the files but the only reason to
> import basestore in localstore.
>
> So rather we define the StoreError class in localstore rather than
> basestore. Its necessary to import localstore in basestore. So this is the
> only way around. I guess.
>
> diff -r 2e11f6756d9c hgext/largefiles/basestore.py
> --- a/hgext/largefiles/basestore.py    Tue Feb 16 14:49:29 2016 +0000
> +++ b/hgext/largefiles/basestore.py    Wed Feb 24 07:25:57 2016 +0530
> @@ -14,23 +14,7 @@
>  from mercurial.i18n import _
>
>  import lfutil
> -
> -class StoreError(Exception):
> -    '''Raised when there is a problem getting files from or putting
> -    files to a central store.'''
> -    def __init__(self, filename, hash, url, detail):
> -        self.filename = filename
> -        self.hash = hash
> -        self.url = url
> -        self.detail = detail
> -
> -    def longmessage(self):
> -        return (_("error getting id %s from url %s for file %s: %s\n") %
> -                 (self.hash, util.hidepassword(self.url), self.filename,
> -                  self.detail))
> -
> -    def __str__(self):
> -        return "%s: %s" % (util.hidepassword(self.url), self.detail)
> +import localstore
>
>  class basestore(object):
>      def __init__(self, ui, repo, url):
> @@ -96,7 +80,7 @@
>
>          try:
>              gothash = self._getfile(tmpfile, filename, hash)
> -        except StoreError as err:
> +        except localstore.StoreError as err:
>              self.ui.warn(err.longmessage())
>              gothash = ""
>          tmpfile.close()
> @@ -160,7 +144,7 @@
>          '''
>          raise NotImplementedError('abstract method')
>
> -import localstore, wirestore
> +import wirestore
>
>  _storeprovider = {
>      'file':  [localstore.localstore],
> diff -r 2e11f6756d9c hgext/largefiles/localstore.py
> --- a/hgext/largefiles/localstore.py    Tue Feb 16 14:49:29 2016 +0000
> +++ b/hgext/largefiles/localstore.py    Wed Feb 24 07:25:57 2016 +0530
> @@ -11,9 +11,25 @@
>  from mercurial.i18n import _
>
>  import lfutil
> -import basestore
>
> -class localstore(basestore.basestore):
> +class StoreError(Exception):
> +    '''Raised when there is a problem getting files from or putting
> +    files to a central store.'''
> +    def __init__(self, filename, hash, url, detail):
> +        self.filename = filename
> +        self.hash = hash
> +        self.url = url
> +        self.detail = detail
> +
> +    def longmessage(self):
> +        return (_("error getting id %s from url %s for file %s: %s\n") %
> +                 (self.hash, util.hidepassword(self.url), self.filename,
> +                  self.detail))
> +
> +    def __str__(self):
> +        return "%s: %s" % (util.hidepassword(self.url), self.detail)
> +
> +class localstore():
>      '''localstore first attempts to grab files out of the store in the
> remote
>      Mercurial repository.  Failing that, it attempts to grab the files
> from
>      the user cache.'''
>
> Well now I feel like i going on the right track but still I doubt. After
> running hg locate 'mercurial/**.py' 'hgext/**.py' | sed 's-\\-/-g' | python
> "$import_checker" - I am getting new import cycles so I think i fixed this
> one.
>
> Regards
> Pulkit Goyal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20160224/8577aefe/attachment.html>


More information about the Mercurial-devel mailing list