This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: changing directories

55 replies
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: changing directories

You mean you can listen on a folder for changes?

-------------------------------------
Ishaaq Chandy wrote:

Not that I can tell from the Javadocs.

However I am pretty excited about it (when it finally arrives) - being able
to abstract the filesystem and create mock ones for testing will be very
handy, plus we can finally get hold of things like file attributes,
permissions, poll asynchronously for filesystem events, etc - IMO these
deficiencies in the current JVMs are much much more troubling than not being
able to change the current working directory.

Ishaaq

On 5 February 2010 05:29, Naftoli Gugenheim wrote:

> Here's another thought/angle: Java is not cross-platform. It's platform
> independent.
> Meaning, changing directories is one of many very OS-ish low-level things
> that Java doesn't make easy.
> That said, one thing no one mentioned: I believe JDK 7 adds a new file API
> which is really meant to address this kind of gap: OS-implemented file
> copying, for example, rather than reading and writing to manually copy
> yourself. Does anyone know if that includes changing directories?
>
>
> -------------------------------------
> Russ Paielli wrote:
>
> On Thu, Feb 4, 2010 at 2:13 AM, Ishaaq Chandy wrote:
>
> >
> > Changing the current working directory is almost always a bad idea in a
> > program that runs on the JVM that can potentially spawn multiple threads
> of
> > work each of which may be trying to do quite different things.
> >
> >
> Look, guys, I'm talking about short data-analysis "scripts" of a few
> hundred
> lines or less that nobody except me will ever see or use. They typically
> run
> in a few seconds. Using multiple threads for something like that would be
> silly. And yes, I agree that there are many ways to avoid changing
> directories. I have many such scripts. In some of them I change the working
> directory, and in some of them I don't. It just depends on the particulars
> and how I feel that day.
>
> Maybe it's because I'm so familiar with Python, but I'm just surprised that
> a major language with massive libraries wouldn't have a simple method to
> change directories. Sure, it could be misused, but so can most language
> features. I thought that one of goals of Scala was to be useful as a
> "scripting" language, but in my view, not being able to change directories
> easily is a significant drawback for scripting convenience. Note that I did
> not say it is a "major" drawback. I said it is "significant." (Come to
> think
> of it, just what the hell does "significant" mean anyway? I'll have to look
> it up one of these days!)
>
> Russ P.
>

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: changing directories
Yes. Finally.
http://download.java.net/jdk7/docs/api/java/nio/file/WatchService.html

Exciting isn't it?

On 5 February 2010 12:13, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:
You mean you can listen on a folder for changes?

-------------------------------------
Ishaaq Chandy<ishaaq [at] gmail [dot] com> wrote:

Not that I can tell from the Javadocs.

However I am pretty excited about it (when it finally arrives) - being able
to abstract the filesystem and create mock ones for testing will be very
handy, plus we can finally get hold of things like file attributes,
permissions, poll asynchronously for filesystem events, etc - IMO these
deficiencies in the current JVMs are much much more troubling than not being
able to change the current working directory.

Ishaaq

On 5 February 2010 05:29, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:

> Here's another thought/angle: Java is not cross-platform. It's platform
> independent.
> Meaning, changing directories is one of many very OS-ish low-level things
> that Java doesn't make easy.
> That said, one thing no one mentioned: I believe JDK 7 adds a new file API
> which is really meant to address this kind of gap: OS-implemented file
> copying, for example, rather than reading and writing to manually copy
> yourself. Does anyone know if that includes changing directories?
>
>
> -------------------------------------
> Russ Paielli<russ [dot] paielli [at] gmail [dot] com> wrote:
>
> On Thu, Feb 4, 2010 at 2:13 AM, Ishaaq Chandy <ishaaq [at] gmail [dot] com> wrote:
>
> >
> > Changing the current working directory is almost always a bad idea in a
> > program that runs on the JVM that can potentially spawn multiple threads
> of
> > work each of which may be trying to do quite different things.
> >
> >
> Look, guys, I'm talking about short data-analysis "scripts" of a few
> hundred
> lines or less that nobody except me will ever see or use. They typically
> run
> in a few seconds. Using multiple threads for something like that would be
> silly. And yes, I agree that there are many ways to avoid changing
> directories. I have many such scripts. In some of them I change the working
> directory, and in some of them I don't. It just depends on the particulars
> and how I feel that day.
>
> Maybe it's because I'm so familiar with Python, but I'm just surprised that
> a major language with massive libraries wouldn't have a simple method to
> change directories. Sure, it could be misused, but so can most language
> features. I thought that one of goals of Scala was to be useful as a
> "scripting" language, but in my view, not being able to change directories
> easily is a significant drawback for scripting convenience. Note that I did
> not say it is a "major" drawback. I said it is "significant." (Come to
> think
> of it, just what the hell does "significant" mean anyway? I'll have to look
> it up one of these days!)
>
> Russ P.
>

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: changing directories
No, I wouldn't use that approach myself, it was just an option I threw out as a possibility - clearly not viable as David kindly pointed out.

However - is a reasonable solution still unclear? I thought we'd given Russ multiple options all of which seemed reasonable enough to me.

My point in my last post was that if he didn't really like any of the other solutions and is adamant on using a Python-like approach, he could still fallback to writing a DSL. He'd have to mirror Python's API and instead of using a construct like:

new java.io.File("foo.txt")
 
- he'd use his DSL (which would have its own concept of the "current directory" - independent of "user.dir") to create the file. In fact one of the previous posters even went so far as to write example code and post it for him.

Ishaaq

On 5 February 2010 11:59, Tony Morris <tonymorris [at] gmail [dot] com> wrote:
Even if the JVM could, a reasonable solution to this problem (which is
still unclear) would still not use such a device.

David Hall wrote:
> Yes. It was more for Russ, as more evidence for
> it-really-can't-be-fixed-on-the-jvm .
>
> -- David
>
> On Thu, Feb 4, 2010 at 4:27 PM, Ishaaq Chandy <ishaaq [at] gmail [dot] com> wrote:
>
>> Which makes sense doesn't it? Even as I wrote that, the thought did cross my
>> mind briefly, but I couldn't be bothered checking.
>>
>> On 5 February 2010 11:16, David Hall <dlwh [at] cs [dot] berkeley [dot] edu> wrote:
>>
>>> On Thu, Feb 4, 2010 at 3:56 PM, Ishaaq Chandy <ishaaq [at] gmail [dot] com> wrote:
>>>
>>>> 3. Stick to Python (or Jython if you want to use the JVM - which sounds
>>>> unlikely to me).
>>>>
>>> FWIW, Jython doesn't have os.chdir
>>>
>>> -- David
>>>
>>>
>>>> Ishaaq
>>>>
>>
>
>


--
Tony Morris
http://tmorris.net/



Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: changing directories

Yup. I'm guessing it can only watch while it's running, but using a start-as-service wrapper you could make e.g. an app that analyzes your hard drive. Once it completes the first time it can wait for events, and maybe rescan gradually on idle cpu...
Btw does the CPS plugin allow you to serialize state? Like can you have subdirectory recursion that pauses when the app exits and resumes where it left off next time you run it?

-------------------------------------
Ishaaq Chandy wrote:

Yes. Finally.
http://download.java.net/jdk7/docs/api/java/nio/file/WatchService.html

Exciting isn't it?

On 5 February 2010 12:13, Naftoli Gugenheim wrote:

> You mean you can listen on a folder for changes?
>
> -------------------------------------
> Ishaaq Chandy wrote:
>
> Not that I can tell from the Javadocs.
>
> However I am pretty excited about it (when it finally arrives) - being able
> to abstract the filesystem and create mock ones for testing will be very
> handy, plus we can finally get hold of things like file attributes,
> permissions, poll asynchronously for filesystem events, etc - IMO these
> deficiencies in the current JVMs are much much more troubling than not
> being
> able to change the current working directory.
>
> Ishaaq
>
> On 5 February 2010 05:29, Naftoli Gugenheim wrote:
>
> > Here's another thought/angle: Java is not cross-platform. It's platform
> > independent.
> > Meaning, changing directories is one of many very OS-ish low-level things
> > that Java doesn't make easy.
> > That said, one thing no one mentioned: I believe JDK 7 adds a new file
> API
> > which is really meant to address this kind of gap: OS-implemented file
> > copying, for example, rather than reading and writing to manually copy
> > yourself. Does anyone know if that includes changing directories?
> >
> >
> > -------------------------------------
> > Russ Paielli wrote:
> >
> > On Thu, Feb 4, 2010 at 2:13 AM, Ishaaq Chandy wrote:
> >
> > >
> > > Changing the current working directory is almost always a bad idea in a
> > > program that runs on the JVM that can potentially spawn multiple
> threads
> > of
> > > work each of which may be trying to do quite different things.
> > >
> > >
> > Look, guys, I'm talking about short data-analysis "scripts" of a few
> > hundred
> > lines or less that nobody except me will ever see or use. They typically
> > run
> > in a few seconds. Using multiple threads for something like that would be
> > silly. And yes, I agree that there are many ways to avoid changing
> > directories. I have many such scripts. In some of them I change the
> working
> > directory, and in some of them I don't. It just depends on the
> particulars
> > and how I feel that day.
> >
> > Maybe it's because I'm so familiar with Python, but I'm just surprised
> that
> > a major language with massive libraries wouldn't have a simple method to
> > change directories. Sure, it could be misused, but so can most language
> > features. I thought that one of goals of Scala was to be useful as a
> > "scripting" language, but in my view, not being able to change
> directories
> > easily is a significant drawback for scripting convenience. Note that I
> did
> > not say it is a "major" drawback. I said it is "significant." (Come to
> > think
> > of it, just what the hell does "significant" mean anyway? I'll have to
> look
> > it up one of these days!)
> >
> > Russ P.
> >
>

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: changing directories
Sorry, no idea about CPS.

You're right, the WatchService can only watch while running, since, in effect, it is reliant on the OS/Filesystem sending events and if there's nothing listening for those events they will be lost.

The other thing is, this is highly OS/FileSystem dependant, not everything will support it, for e.g. it is hard to imagine an HTTP-based FileSystem implementation being able to support this efficiently. Or even legacy FAT32 systems either (though I could be wrong on that score).

However, where it is supported it will be way more efficient than constant synchronous directory scanning - which is your only option pre-Java7. And yes, you should be able to merge the two approaches to good effect to cater for situations where the service shuts down and loses event notifications.

Ishaaq

On 5 February 2010 12:25, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:
Yup. I'm guessing it can only watch while it's running, but using a start-as-service wrapper you could make e.g. an app that analyzes your hard drive. Once it completes the first time it can wait for events, and maybe rescan gradually on idle cpu...
Btw does the CPS plugin allow you to serialize state? Like can you have subdirectory recursion that pauses when the app exits and resumes where it left off next time you run it?

-------------------------------------
Ishaaq Chandy<ishaaq [at] gmail [dot] com> wrote:

Yes. Finally.
http://download.java.net/jdk7/docs/api/java/nio/file/WatchService.html

Exciting isn't it?

On 5 February 2010 12:13, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:

> You mean you can listen on a folder for changes?
>
> -------------------------------------
> Ishaaq Chandy<ishaaq [at] gmail [dot] com> wrote:
>
> Not that I can tell from the Javadocs.
>
> However I am pretty excited about it (when it finally arrives) - being able
> to abstract the filesystem and create mock ones for testing will be very
> handy, plus we can finally get hold of things like file attributes,
> permissions, poll asynchronously for filesystem events, etc - IMO these
> deficiencies in the current JVMs are much much more troubling than not
> being
> able to change the current working directory.
>
> Ishaaq
>
> On 5 February 2010 05:29, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:
>
> > Here's another thought/angle: Java is not cross-platform. It's platform
> > independent.
> > Meaning, changing directories is one of many very OS-ish low-level things
> > that Java doesn't make easy.
> > That said, one thing no one mentioned: I believe JDK 7 adds a new file
> API
> > which is really meant to address this kind of gap: OS-implemented file
> > copying, for example, rather than reading and writing to manually copy
> > yourself. Does anyone know if that includes changing directories?
> >
> >
> > -------------------------------------
> > Russ Paielli<russ [dot] paielli [at] gmail [dot] com> wrote:
> >
> > On Thu, Feb 4, 2010 at 2:13 AM, Ishaaq Chandy <ishaaq [at] gmail [dot] com> wrote:
> >
> > >
> > > Changing the current working directory is almost always a bad idea in a
> > > program that runs on the JVM that can potentially spawn multiple
> threads
> > of
> > > work each of which may be trying to do quite different things.
> > >
> > >
> > Look, guys, I'm talking about short data-analysis "scripts" of a few
> > hundred
> > lines or less that nobody except me will ever see or use. They typically
> > run
> > in a few seconds. Using multiple threads for something like that would be
> > silly. And yes, I agree that there are many ways to avoid changing
> > directories. I have many such scripts. In some of them I change the
> working
> > directory, and in some of them I don't. It just depends on the
> particulars
> > and how I feel that day.
> >
> > Maybe it's because I'm so familiar with Python, but I'm just surprised
> that
> > a major language with massive libraries wouldn't have a simple method to
> > change directories. Sure, it could be misused, but so can most language
> > features. I thought that one of goals of Scala was to be useful as a
> > "scripting" language, but in my view, not being able to change
> directories
> > easily is a significant drawback for scripting convenience. Note that I
> did
> > not say it is a "major" drawback. I said it is "significant." (Come to
> > think
> > of it, just what the hell does "significant" mean anyway? I'll have to
> look
> > it up one of these days!)
> >
> > Russ P.
> >
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland